<?php
/**
 *
 * User: anker
 * Date: 4/22/22
 * Email: <13408046898@163.com>
 **/

namespace app\api\middleware;

class Auth
{
    public function handle($request, \Closure $next)
    {
//        前置中间件
        $accessToken = $request->header('token')??'';
        $userInfo = cache(config('common.token_pre') . $accessToken);
//        判断用户是否存在
        if (empty($userInfo)) {
            return show(config('common.http_status.not_login'), '没有登录');
        } else {
//            $adminObj = new \app\common\business\Admin();
//            $adminObj->getInfo($userInfo['id']);
            $request->userInfo = $userInfo;
            $request->token = $accessToken;
        }
        $response = $next($request);
//        后置中间件
        return $response;
    }

    /**
     * 中间件结束调度
     * @param \think\Response $response
     * @return void
     */
    public function end(\think\Response $response)
    {

    }
}